#include <iostream>
#include <Windows.h>

using namespace std;

HWND wd;
HHOOK hook;

//捕获F1-F8,映射发送到被控窗口的1-8. 
LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam){
	
	DWORD vk_code = ((KBDLLHOOKSTRUCT*)lParam)->vkCode;
	if(vk_code < 112 || vk_code > 119){
		return CallNextHookEx(hook, nCode, wParam, lParam);	
	}else{
		cout << "vk_code = " << vk_code << endl;
	}
	
	SendMessage(wd, wParam, vk_code-63 , 1);
	return CallNextHookEx(hook, nCode, wParam, lParam);
}

int main(int argc, char** argv) {
	//获取窗口
	wd = FindWindow(NULL,"精神控制");
	if(wd == NULL){
		wd = FindWindow(NULL,"魔兽世界");
		if(wd != NULL){
			cout << "发现<魔兽世界>客户端,并精神控制." << endl; 
			SetWindowTextA(wd,"精神控制");	
		}else{
			cout << "未找到<魔兽世界>客户端,结束运行.";
			Sleep(3000);
			return 0;
		}
	}else{
		cout << "发现<魔兽世界>客户端,并精神控制." << endl; 
	}
	

	//捕获键盘
	hook = SetWindowsHookExA(WH_KEYBOARD_LL, (HOOKPROC)&KeyboardProc, GetModuleHandle(NULL), 0);
	if(hook){
		cout << "捕获键盘成功." << endl; 
	}else{
		cout << "键盘捕获失败,结束运行.";
		Sleep(3000);
		return 0;
	}
	
	//消息循环
	MSG msg;
    while (GetMessage (&msg, NULL, 0, 0)!=-1)
    {
        TranslateMessage (&msg);
        DispatchMessage (&msg);
    };
	
	return 0;
}
